package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huawei.hmsdemo10086.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Apply_For_VIPActivity extends AppCompatActivity {

    TextView realName;
    TextView IDCard;
    Button apply_button;
    private SharedPreferences sharedPreferences;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_vip);

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);

        //TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Apply_For_VIPActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        realName = (TextView)findViewById(R.id.realname);
        IDCard = (TextView)findViewById(R.id.IDCard);
        apply_button = (Button)findViewById(R.id.apply);

        apply_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                apply();
                Intent intent = new Intent(Apply_For_VIPActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    public void apply()
    {
        String url = "http://10.0.2.2:8443/api/updateVIP";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\",\n" +
                "  \"realname\": \"" + realName.getText().toString() + "\",\n" +
                "  \"idCard\": \"" + IDCard.getText().toString() + "\"\n" +
                "}";
        System.out.println(json);


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                SharedPreferences sharedPreference=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreference.edit();
                editor.putBoolean("isVIP",true);
                editor.commit();
            }
        });
    }





}